//
//  Js_LogView.m
//  MixedPlayerSdkDemo
//
//  Created by Gikki Ares on 2022/12/16.
//

#import "JsLogoView.h"
#import "JsGeometryManager.h"

@interface JsLogoView()

{
	UIImageView * miv_logo;
	UILabel * mlb_companyName;
	UILabel * mlb_info;
	
	
	//FixedUiData
	float mi_width,mf_hieght;
	int mi_imageSize,mi_imageTopMargin;
	int mi_companyNameTopMargin,mi_companyNameHeight;
	int mi_infoTopMargin,mi_infoBottomMargin;
	float mf_infoHeight;

}

@end

@implementation JsLogoView
//init会调用initWithFrame,避免重复调用.
//- (instancetype)init {
//	if(self = [super init]) {
//		[self commonInit];
//	}
//	return self;
//}

//- (void)awakeFromNib {
//	[super awakeFromNib];
//	[self commonInit];
//}

- (instancetype)initWithCoder:(NSCoder *)coder {
	if(self = [super initWithCoder:coder]) {
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
	if(self = [super initWithFrame:frame]) {
		[self commonInit];
	}
	return self;
}

- (void)layoutSubviews {
	[super layoutSubviews];
	[self setupUi];
}

/**
 内容数据(外部可调)
 +
 动态样式数据(外部可调)
 +
 固定样式数据(不可调)
 =
 最终结果
 */
- (void)setFixedUiData {
	mi_imageSize = 100;
	mi_imageTopMargin = 10;
	mi_companyNameTopMargin = 10;
	mi_companyNameHeight = 20;
	mi_infoTopMargin = 10;
	mf_infoHeight = 70;
	mi_infoBottomMargin = 10;
}

- (void)setupUi {
	mi_width = self.bounds.size.width;
	mf_hieght = self.bounds.size.height;
	miv_logo.image = self.image;
	float x,y,w,h;
	{
		x = (mi_width - mi_imageSize)/2;
		y = mi_imageTopMargin;
		w = h = mi_imageSize;
		miv_logo.frame = CGRectMake(x,y,w,h);
	}
	
	{
		x = 0;
		y += mi_imageSize + mi_companyNameTopMargin;
		h = mi_companyNameHeight;
		w = mi_width;
		mlb_companyName.frame = CGRectMake(x,y,w,h);
		mlb_companyName.text = self.companyName;
		mlb_companyName.textAlignment = NSTextAlignmentCenter;
		mlb_companyName.font = [UIFont systemFontOfSize:17 weight:UIFontWeightBold];
	}
	
	{
		x = 0;
		y += mi_companyNameHeight + mi_infoTopMargin;
		UIFont * font_info = [UIFont systemFontOfSize:12];
		mf_infoHeight = [JsGeometryManager sizeForMultiLineStringWithContext:self.info font:font_info maxWidth:mi_width maxHeight:0].height;
		h = mf_infoHeight;
		
		w = mi_width;
		mlb_info.frame = CGRectMake(x,y,w,h);
		
		mlb_info.text = self.info;
		mlb_info.font = font_info;
		mlb_info.textAlignment = NSTextAlignmentCenter;
	}
	h = [self wrapContentHeight];
	self.bounds = CGRectMake(0, 0, mi_width, h);
}
- (void)onTap:(UITapGestureRecognizer *)gesture {
		//默认最大值为M_PI,如果要完整动画,需要分为两个部分去完成.
	
	float x = arc4random_uniform(1000)/1000.0 * 0;
	float y = arc4random_uniform(1000)/1000.0 * 0;
	float z = arc4random_uniform(1000)/1000.0 * 1;
	
	[UIView animateWithDuration:1 animations:^{
		//弧度
		CATransform3D transform = CATransform3DMakeRotation( M_PI, x, y, z);
		miv_logo.layer.transform = transform;
	} completion:^(BOOL finished) {
		if(finished) {
//			NSLog(@"step 1 finished");
			[UIView animateWithDuration:1 animations:^{
				CATransform3D transform = CATransform3DMakeRotation( 0, x, y, z);
				miv_logo.layer.transform = transform;
			} completion:^(BOOL finished) {
				if(finished) {
//					NSLog(@"step 2 finished");
					miv_logo.layer.transform = CATransform3DIdentity;
				}
			}];
		}
	}];
	
}

- (void)cabasicAnimation {
	CABasicAnimation *theAnimation;
	theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
	theAnimation.duration=1;
	theAnimation.removedOnCompletion = 1;
	theAnimation.fromValue = [NSNumber numberWithFloat:0];
	theAnimation.toValue = [NSNumber numberWithFloat:3.1415926];
	[miv_logo.layer addAnimation:theAnimation forKey:@"animateTransform"];
}

- (void)commonInit {
	miv_logo = [[UIImageView alloc]init];
	miv_logo.userInteractionEnabled = YES;
	UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
	[miv_logo addGestureRecognizer:tap];
	mlb_companyName = [[UILabel alloc]init];
	mlb_info = [[UILabel alloc]init];
	mlb_info.numberOfLines = 0;
	
	[self addSubview:miv_logo];
	[self addSubview:mlb_companyName];
	[self addSubview:mlb_info];
	
	[self setFixedUiData];
}

- (float) wrapContentHeight {
	float h = 0;
	h += mi_imageTopMargin;
	h += mi_imageSize;
	h += mi_companyNameTopMargin;
	h += mi_companyNameHeight;
	h += mi_infoTopMargin;
	h += mf_infoHeight;
	h += mi_infoBottomMargin;
	return h;
}

@end
